﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1.Interface
{
    public class TimeAndStart
    {

        public string DisplayedTime { private set; get; }
        public double StartedTime { private set; get; }
        public double RunningTime { set; get; }
        public bool MustGetTime { set; get; }
        public bool IsVisible { set; get; }

        public TimeAndStart()
        {

            MustGetTime = true;
            RunningTime = 0;
            StartedTime = 0;
            DisplayedTime = "00:00:00";
            Visible(false);
            State = GameState.Stopped;
        }

        public GameState State { private set; get; }

        public enum GameState
        {
            Started,
            Stopped,
            Finished,
            NONE
        }

        public void Update(GameTime gameTime)
        {
            switch (State)
            {
                case GameState.Started:
                    {
                        if (MustGetTime)
                        {
                            StartedTime = gameTime.TotalGameTime.TotalMilliseconds;
                            MustGetTime = false;
                        }

                        RunningTime += gameTime.ElapsedGameTime.Milliseconds;

                        double run = RunningTime/1000;

                        var minutes = (int) (run/60);
                        var seconds = (int) run%60;
                        var millis = (int) (((run - (minutes*60)) - seconds)*100);

                        DisplayedTime =
                            (minutes < 10 ? "0" + minutes : minutes.ToString()) + ":" +
                            (seconds < 10 ? "0" + seconds : seconds.ToString()) + ":" +
                            (millis < 10 ? "0" + millis : millis.ToString());
                        break;
                    }
                case GameState.Stopped:
                    {

                        break;
                    }
                default :
                    break;
            }
        }

        public void StartGame()
        {
            ChangeState(GameState.Started);
        }

        public void FinishGame()
        {
            ChangeState(GameState.Finished);
        }

        public void ChangeState(GameState toThisState)
        {
            State = toThisState;

            switch(State)
            {
                case GameState.Started:
                case GameState.Stopped:
                    Visible(true);
                    break;
                case GameState.Finished:
                case GameState.NONE:
                    Visible(false);
                    break;
            }
        }

        public void Render(SpriteBatch render)
        {
            if(IsVisible)
                render.DrawString(StaticGUi.Font, DisplayedTime, new Vector2(StaticGUi.Width/2-100, 0), Color.White);
        }

        public void Visible(bool visible)
        {
            IsVisible = visible;
        }
    }
}
